package com.boc.ewr.work.aspect;

import com.boc.ewr.util.file.enumeration.ImportEnum;
import com.boc.ewr.work.entity.WorkOprateRecord;
import com.boc.ewr.work.service.IWorkOprateRecordService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.event.IDFilter;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.SysDepartModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author : Wzz
 * @create : 2023-06- 19- 17:08
 * @desc :crud导入监控切面 上传（新增）、下载（导出）、变更（编辑）
 */
@Aspect
@Component
@Slf4j
public class WorkOprateRecordCrudAspect {

    @Autowired
    private IWorkOprateRecordService workOprateRecordService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    // 上传（新增）、下载（导出）、变更（编辑）
    @Around("execution(* com..*Controller.add(..)) || " +
            "execution(* com..*Controller.edit(..)) || " +
            "execution(* com..*Controller.exportXls(..)) ||" +
            " execution(* com..*Controller.delete(..)) ||" +
            " execution(* com..*Controller.deleteBatch(..))")
    public Object logMethodCall(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取方法开始时间
        long startTime = System.currentTimeMillis();
        // 继续执行被拦截的方法
        Object proceed = joinPoint.proceed();
        // 获取调用类名、方法名
        StringBuilder fncNm = new StringBuilder();

        Api apiAnnotation = joinPoint.getTarget().getClass().getAnnotation(Api.class);
        fncNm.append(apiAnnotation.tags()[0]);
        String methodName = joinPoint.getSignature().getName();
        if ("add".equals(methodName)){
            fncNm.append("新增");
        }else if ("edit".equals(methodName)){
            fncNm.append("修改");
        }else if ("exportXls".equals(methodName)){
            fncNm.append("导出");
        }else if ("delete".equals(methodName)){
            fncNm.append("删除");
        }else if ("deleteBatch".equals(methodName)){
            fncNm.append("批量删除");
        }
        // 获取方法结束时间
        long endTime = System.currentTimeMillis();
        // 插入调用信息到数据库
        WorkOprateRecord workData = new WorkOprateRecord();
        LoginUser loginUser = this.getLoginUser();
        SysDepartModel sysDepartModel = sysBaseAPI.selectAllById(loginUser.getOrgCode());
        workData.setParentBranchNo(sysDepartModel.getParentId());
        workData.setOrgNo(sysDepartModel.getOrgCode());
        workData.setOrgNm(sysDepartModel.getDepartName());
        workData.setEhrTlrNo(sysBaseAPI.getWorkNoByLoginUser(loginUser));
        workData.setEmpNm(loginUser.getRealname());
        workData.setFncNm(fncNm.toString());
        workData.setStartTime(new Date(startTime));
        workData.setEndTime(new Date(endTime));

        workOprateRecordService.save(workData);

        return proceed;
    }

    // 获取登录用户
    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }
}
